package net.goutalk.glcs.module.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.MPJWrappers;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.system.dto.CodeSchemaPageDto;
import net.goutalk.glcs.module.system.entity.CodeSchema;
import net.goutalk.glcs.module.system.entity.DictionaryDetail;
import net.goutalk.glcs.module.system.mapper.CodeSchemaMapper;
import net.goutalk.glcs.module.system.service.ICodeSchemaService;
import net.goutalk.glcs.module.system.vo.CodeSchemaPageVo;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 代码模板 服务实现类
 * </p>
 *
 * @author zlf
 * @since 2022-09-29
 */
@Service
public class CodeSchemaServiceImpl extends MPJBaseServiceImpl<CodeSchemaMapper, CodeSchema> implements ICodeSchemaService {

    @Override
    public PageOutput<CodeSchemaPageVo> page(CodeSchemaPageDto dto) {
        IPage<CodeSchemaPageVo> page = this.selectJoinListPage(ConventPage.getPage(dto), CodeSchemaPageVo.class,
                MPJWrappers.<CodeSchema>lambdaJoin()
                        .select(CodeSchema::getId)
                        .select(CodeSchema.class, x -> VoToColumnUtil.fieldsToColumns(CodeSchemaPageVo.class).contains(x.getProperty()))
                        .selectAs(DictionaryDetail::getName, CodeSchemaPageVo::getCategoryName)
                        .leftJoin(DictionaryDetail.class, DictionaryDetail::getId, CodeSchema::getCategory)
                        .eq(StrUtil.isNotBlank(dto.getCategory()), CodeSchema::getCategory, dto.getCategory())
                        .eq(dto.getType() != null, CodeSchema::getType, dto.getType())
                        .like(StrUtil.isNotBlank(dto.getKeyword()), CodeSchema::getName, dto.getKeyword())
                        .orderByDesc(CodeSchema::getCreateDate));
        return ConventPage.getPageOutput(page);
    }
}
